# !/usr/bin/env python
# -*- coding: utf-8 -*-
# @File  : 波士顿房价预测（4L1正则化）.py
# @Author: dongguangwen
# @Date  : 2025-02-06 14:06
import numpy as np
from sklearn.linear_model import LinearRegression, Lasso
import matplotlib.pyplot as plt
from sklearn.metrics import mean_squared_error

# 1.准备数据
np.random.seed(22)
x = np.random.uniform(-3, 3, size=100)
# print(x)
y = 0.5 * x ** 2 + np.random.normal(0, 1, size=100)
print(y)

# 2.模型训练
model = Lasso(alpha=0.01)  # 调整alpha 正则化强度 查看正则化效果
X = x.reshape(-1, 1)
X2 = np.hstack([X, X ** 2, X ** 3, X ** 4, X ** 5, X ** 6, X ** 7, X ** 8, X ** 9, X ** 10])  # 数据增加多次项
model.fit(X2, y)
print('权重参数值：', model.coef_)  # Lasso 回归  L1正则 会将高次方项系数变为0

# 3.模型预测
y_pred = model.predict(X2)
print(y_pred)

# 4.计算均方误差
myret = mean_squared_error(y, y_pred)
print('myret-->', myret)

# 5.展示效果
plt.scatter(X, y)
# 画图plot折线图时 需要对x进行排序, 取x排序后对应的y值
plt.plot(np.sort(x), y_pred[np.argsort(x)], color='red')
plt.show()

"""
[ 2.68991269 -0.15896455 -1.0657469   1.77691179  2.2169277   1.44760438
  1.95468165  1.43909861  0.15853376  1.32780734  4.86511909  0.39702949
  2.63475246 -0.14604323  2.36460552  1.62061024  0.83996403  3.84213349
  0.38401779  1.60688722  1.07599777  0.65478898  0.9188096  -0.64424027
  1.59171645 -0.51865919  1.34359201  2.92620657  2.80123304  4.40659537
  0.87811453  1.10113724  6.1923925   0.855368    2.53145803  2.20814387
  0.50914933  3.56598319 -1.52802892 -0.22965108  0.68003709  4.97404497
 -0.75143267  2.88409744  5.60301458  0.99887944  2.62802974  2.46495793
  3.66571442 -1.85804906  0.83140672  0.86165142  0.33581663  0.06046012
  2.05133222  0.89686498  0.570795    4.81741428 -0.24435866  1.68542272
  2.49369326  2.83636823 -0.23086228  2.26822132  2.33381088  0.67749307
  1.59425991  2.65328755  3.70118035  3.9285876   1.95288399  2.60958529
  3.87293405  3.76537917  0.40451931  3.28686949  0.78030988  4.27240723
  2.19597273 -0.5814483   1.44486224  0.43852349 -0.27671756  2.04600828
  4.32094833 -0.20276307  2.15265487  1.69291982  0.10829686  5.84398733
  0.48910081  5.08461625  0.35116797  2.44363252  0.07344753  1.08868863
  1.41343863  2.96338708  4.35563861  1.45501813]
权重参数值： [-1.70710527e-01  5.99255457e-01  1.43876060e-01 -3.69579843e-02
 -3.03747248e-02  7.42219146e-03 -2.04422727e-03  4.91027945e-06
  4.50165200e-04 -6.83376682e-05]
[1.81395294 0.09802399 0.27316844 2.32659385 2.3967614  0.66804973
 1.14528167 0.76947578 1.65940245 1.89033959 4.04780225 0.09660542
 1.90715232 1.24899111 2.09651923 3.84538842 1.50693261 3.38172276
 0.85979705 0.93434605 1.46784417 0.74673503 0.41511186 0.27550179
 0.24703279 0.15445009 0.86571062 3.61549588 2.93660556 3.97642877
 0.66238302 1.73001823 4.5600529  2.28205573 2.46879357 1.24740307
 0.06535867 3.25315657 0.09351892 0.06154695 0.83977953 4.66454472
 0.2118324  3.9638208  4.66502301 0.71980896 2.23603863 3.32149301
 3.23726849 0.11483823 1.96806041 1.59946528 0.24030914 0.19300954
 3.82234365 0.91995127 1.68020569 2.46280632 0.06661291 0.82550466
 2.81370423 4.16067686 0.82121114 1.67512367 1.1698878  2.04616681
 1.24393401 2.28583379 2.32000672 3.81339623 0.56313949 3.31125629
 3.15526651 4.44750183 0.62937255 2.52482775 0.11105548 1.41272481
 2.64324381 0.06884288 0.1196994  0.12159499 0.06530722 0.75923379
 2.57423136 1.31500625 2.35283929 0.37988403 0.82023594 4.57951348
 0.51796852 4.24228144 0.06644551 3.04281663 0.20160234 1.93200884
 1.81328259 1.8496932  4.59605571 1.68325729]
myret--> 0.8933374125912067
"""